package com.cll.learn.cas.demo1;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2021/5/15 11:02
 */
public class RechargeThread implements Runnable {

    private final User user;

    public RechargeThread(User user) {
        this.user = user;
    }

    @Override
    public void run() {
        while (true) {
            while (true) {
                Integer currentMoney = user.getMoney().get();
                if (currentMoney < 20) {
                    if (user.getMoney().compareAndSet(currentMoney, currentMoney + 20)){
                        System.out.println("===>>> 当前用户余额 = " + currentMoney + "：充值之后余额 = " + user.getMoney().get());
                        break;
                    }
                } else {
//                    System.out.println("===>>> 当前账户不需要充值。");
                    break;
                }
            }
        }
    }
}
